<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="国外前端会考察哪些问题？js函数、系统设计、TS类型体操的用法，大大拓宽了我的眼界，去思考和实战更加相关的问题吧，React Problems考察了实用的React技巧，当然，已经是基于16.8版本以上的React hooks了。">
<meta property="og:type" content="article">
<meta property="og:title" content="bigfrontend的React problems">
<meta property="og:url" content="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20problems/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="国外前端会考察哪些问题？js函数、系统设计、TS类型体操的用法，大大拓宽了我的眼界，去思考和实战更加相关的问题吧，React Problems考察了实用的React技巧，当然，已经是基于16.8版本以上的React hooks了。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-06-17T13:07:33.000Z">
<meta property="article:modified_time" content="2022-08-19T01:05:49.980Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="React">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="bigfrontend">
<meta property="article:tag" content="题库">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20problems/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>bigfrontend的React problems | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20problems/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          bigfrontend的React problems
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-06-17 21:07:33" itemprop="dateCreated datePublished" datetime="2022-06-17T21:07:33+08:00">2022-06-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-08-19 09:05:49" itemprop="dateModified" datetime="2022-08-19T09:05:49+08:00">2022-08-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">web前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/06/17/bigfrontend%E7%9A%84React%20problems/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/06/17/bigfrontend%E7%9A%84React%20problems/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>国外前端会考察哪些问题？js函数、系统设计、TS类型体操的用法，大大拓宽了我的眼界，去思考和实战更加相关的问题吧，React Problems考察了实用的React技巧，当然，已经是基于16.8版本以上的React hooks了。</p>
<span id="more"></span>

<h1 id="React-Problems"><a href="#React-Problems" class="headerlink" title="React Problems"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react">React Problems</a></h1><p>该部分综合考察对React的熟悉程度</p>
<h2 id="1-the-React-Counter-app"><a href="#1-the-React-Counter-app" class="headerlink" title="1. the React Counter app"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/The-React-Counter">1. the React Counter app</a></h2><h3 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h3><p>As the first React problem, you are asked to create the famous Counter app.</p>
<ol>
<li>counter starts from 0.</li>
<li>click the ‘+’ button to increment.</li>
<li>click the ‘-‘ button to decrement.</li>
</ol>
<blockquote>
<p><code>data-testid</code> is used to test your code, please keep them.</p>
</blockquote>
<h3 id="我的答案"><a href="#我的答案" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123;useState&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function App() &#123;</span><br><span class="line">  const [count, setCount] &#x3D; useState&lt;number&gt;(0);</span><br><span class="line">   const decrement &#x3D; () &#x3D;&gt; setCount(count - 1);</span><br><span class="line">   const increment &#x3D; () &#x3D;&gt; setCount(count + 1);</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;button data-testid&#x3D;&quot;decrement-button&quot; onClick&#x3D;&#123;decrement&#125;&gt;-&lt;&#x2F;button&gt;</span><br><span class="line">      &lt;button data-testid&#x3D;&quot;increment-button&quot; onClick&#x3D;&#123;increment&#125;&gt;+&lt;&#x2F;button&gt;</span><br><span class="line">      &lt;p&gt;clicked: &#123;count&#125;&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; run your code by clicking the run button on the right</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="2-useTimeout"><a href="#2-useTimeout" class="headerlink" title="2. useTimeout()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/usetimeout">2. useTimeout()</a></h2><h3 id="题目-1"><a href="#题目-1" class="headerlink" title="题目"></a>题目</h3><p>Create a hook to easily use <code>setTimeout(callback, delay)</code>.</p>
<ol>
<li>reset the timer if delay changes</li>
<li>DO NOT reset the timer if only callback changes</li>
</ol>
<h3 id="我的答案-1"><a href="#我的答案-1" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import &#123;useEffect, useRef&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useTimeout(callback: () &#x3D;&gt; void, delay: number) &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const callbackRef &#x3D; useRef(callback);</span><br><span class="line">  callbackRef.current &#x3D; callback;</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    const id &#x3D; setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      callbackRef.current();</span><br><span class="line">    &#125;, delay);</span><br><span class="line">    return () &#x3D;&gt; clearTimeout(id);</span><br><span class="line">  &#125;, [delay])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; if you want to try your code on the right panel</span><br><span class="line">&#x2F;&#x2F; remember to export App() component like below</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; export function App() &#123;</span><br><span class="line">&#x2F;&#x2F;   return &lt;div&gt;your app&lt;&#x2F;div&gt;</span><br><span class="line">&#x2F;&#x2F; &#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-useIsFirstRender"><a href="#3-useIsFirstRender" class="headerlink" title="3. useIsFirstRender()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useIsFirstRender">3. useIsFirstRender()</a></h2><h3 id="题目-2"><a href="#题目-2" class="headerlink" title="题目"></a>题目</h3><p>Create a hook to tell if it is the first render.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function App() &#123;</span><br><span class="line">  const isFirstRender &#x3D; useIsFirstRender()</span><br><span class="line">  &#x2F;&#x2F; only true for the first render</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="我的答案-2"><a href="#我的答案-2" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">import &#123;useEffect, useRef&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useIsFirstRender(): boolean &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const isFirstRender &#x3D; useRef(true);</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    isFirstRender.current &#x3D; false;</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  return isFirstRender.current;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; if you want to try your code on the right panel</span><br><span class="line">&#x2F;&#x2F; remember to export App() component like below</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; export function App() &#123;</span><br><span class="line">&#x2F;&#x2F;   return &lt;div&gt;your app&lt;&#x2F;div&gt;</span><br><span class="line">&#x2F;&#x2F; &#125;</span><br></pre></td></tr></table></figure>

<p>或者不用useEffect</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">import &#123;useRef&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useIsFirstRender(): boolean &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const isFirstRender &#x3D; useRef(true);</span><br><span class="line"></span><br><span class="line">  if (isFirstRender.current) &#123;</span><br><span class="line">    isFirstRender.current &#x3D; false;</span><br><span class="line">    return true;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  return false;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="4-useSWR-I"><a href="#4-useSWR-I" class="headerlink" title="4. useSWR() I"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useSWR-1">4. useSWR() I</a></h2><h3 id="题目-3"><a href="#题目-3" class="headerlink" title="题目"></a>题目</h3><p><a target="_blank" rel="noopener" href="https://swr.vercel.app/">SWR</a> is a popular library of data fetching.</p>
<p>Let’s try to implement the basic usage by ourselves.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import React from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const &#123; data, error &#125; &#x3D; useSWR(&#39;&#x2F;api&#39;, fetcher)</span><br><span class="line">  if (error) return &lt;div&gt;failed&lt;&#x2F;div&gt;</span><br><span class="line">  if (!data) return &lt;div&gt;loading&lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line">  return &lt;div&gt;succeeded&lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li>this is not to replicate the true implementation of <code>useSWR()</code></li>
<li>The first argument <code>key</code> is for deduplication, we can safely ignore it for now</li>
</ol>
<h3 id="我的答案-3"><a href="#我的答案-3" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">import &#123;useState, useEffect&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useSWR&lt;T &#x3D; any, E &#x3D; any&gt;(</span><br><span class="line">  _key: string,</span><br><span class="line">  fetcher: () &#x3D;&gt; T | Promise&lt;T&gt;</span><br><span class="line">): &#123;</span><br><span class="line">  data?: T</span><br><span class="line">  error?: E</span><br><span class="line">&#125; &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const fetcherRes &#x3D; fetcher();</span><br><span class="line">  &#x2F;&#x2F; 同步</span><br><span class="line">  const [data, setData] &#x3D; useState&lt;T | undefined&gt;(fetcherRes instanceof Promise ? undefined : fetcherRes);</span><br><span class="line">  const [error, setError] &#x3D; useState&lt;E | undefined&gt;(undefined);</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    &#x2F;&#x2F; 异步</span><br><span class="line">    if (fetcherRes instanceof Promise) &#123;</span><br><span class="line">      Promise.resolve(fetcherRes)</span><br><span class="line">      .then(res &#x3D;&gt; setData(res))</span><br><span class="line">      .catch(err &#x3D;&gt; setError(err))</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [])</span><br><span class="line">  return &#123;</span><br><span class="line">    data,</span><br><span class="line">    error</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用useMemo</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useEffect, useMemo, useState &#125; from &quot;react&quot;;</span><br><span class="line"></span><br><span class="line">export function useSWR&lt;T &#x3D; any, E &#x3D; any&gt;(</span><br><span class="line">  _key: string,</span><br><span class="line">  fetcher: () &#x3D;&gt; T | Promise&lt;T&gt;</span><br><span class="line">): &#123;</span><br><span class="line">  data?: T</span><br><span class="line">  error?: E</span><br><span class="line">&#125; &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const [data, setData] &#x3D; useState&lt;T&gt;();</span><br><span class="line">  const [error, setError] &#x3D; useState&lt;E&gt;();</span><br><span class="line">  const result &#x3D; useMemo(fetcher, [_key]);</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    if (result instanceof Promise) &#123;</span><br><span class="line">      result.then(setData, setError);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  return &#123;data: result instanceof Promise ? data : result, error&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="5-usePrevious"><a href="#5-usePrevious" class="headerlink" title="5. usePrevious()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useSWR-1">5. usePrevious()</a></h2><h3 id="题目-4"><a href="#题目-4" class="headerlink" title="题目"></a>题目</h3><p>Create a hook <code>usePrevious()</code> to return the previous value, with initial value of `undefined.</p>
<h3 id="我的答案-4"><a href="#我的答案-4" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import &#123;useRef, useEffect&#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function usePrevious&lt;T&gt;(value: T): T | undefined &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const previos &#x3D; useRef&lt;T&gt;();</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    previos.current &#x3D; value;</span><br><span class="line">  &#125;, [value])</span><br><span class="line"></span><br><span class="line">  return previos.current;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="6-useHover"><a href="#6-useHover" class="headerlink" title="6. useHover()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useHover">6. useHover()</a></h2><h3 id="题目-5"><a href="#题目-5" class="headerlink" title="题目"></a>题目</h3><p>It is common to see conditional rendering based on hover state of some element.</p>
<p>We can achive it by CSS pseduo class <code>:hover</code>, but for more complex cases it might be better to have state controlled by script.</p>
<p>Now you are asked to create a <code>useHover()</code> hook.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function App() &#123;</span><br><span class="line">  const [ref, isHovered] &#x3D; useHover()</span><br><span class="line">  return &lt;div ref&#x3D;&#123;ref&#125;&gt;&#123;isHovered ? &#39;hovered&#39; : &#39;not hovered&#39;&#125;&lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="我的答案-5"><a href="#我的答案-5" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useRef, useState, useCallback, Ref &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useHover&lt;T extends HTMLElement&gt;(): [Ref&lt;T&gt;, boolean] &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const [isHovered, setIsHovered] &#x3D; useState&lt;boolean&gt;(false);</span><br><span class="line">  const handleMouseEnter &#x3D; useCallback(() &#x3D;&gt; &#123;setIsHovered(true)&#125;, []);</span><br><span class="line">  const handleMouseLeave &#x3D; useCallback(() &#x3D;&gt; &#123;setIsHovered(false)&#125;, []);</span><br><span class="line"></span><br><span class="line">  const ref &#x3D; useRef&lt;T&gt;();</span><br><span class="line"></span><br><span class="line">  const callbackRef &#x3D; useCallback((node: T) &#x3D;&gt; &#123;</span><br><span class="line">    if (ref.current) &#123;</span><br><span class="line">      ref.current.removeEventListener(&#39;mouseenter&#39;, handleMouseEnter);</span><br><span class="line">      ref.current.removeEventListener(&#39;mouseleave&#39;, handleMouseLeave);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ref.current &#x3D; node;</span><br><span class="line">    if (ref.current) &#123;</span><br><span class="line">      ref.current.addEventListener(&#39;mouseenter&#39;, handleMouseEnter);</span><br><span class="line">      ref.current.addEventListener(&#39;mouseleave&#39;, handleMouseLeave);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [handleMouseEnter, handleMouseLeave]);</span><br><span class="line">  return [callbackRef, isHovered];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="7-useToggle"><a href="#7-useToggle" class="headerlink" title="7. useToggle()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useToggle">7. useToggle()</a></h2><h3 id="题目-6"><a href="#题目-6" class="headerlink" title="题目"></a>题目</h3><p>It is quite common to see switches and checkboxes in web apps.</p>
<p>Implement <code>useToggle()</code> to make things easier</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function App() &#123;</span><br><span class="line">  const [on, toggle] &#x3D; useToggle()</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="我的答案-6"><a href="#我的答案-6" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useState, useCallback &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useToggle(on: boolean): [boolean, () &#x3D;&gt; void] &#123;</span><br><span class="line">  &#x2F;&#x2F; your code here</span><br><span class="line">  const [toggle, setToggle] &#x3D; useState(on);</span><br><span class="line">  const toggleHandler &#x3D; useCallback(() &#x3D;&gt; &#123;</span><br><span class="line">    setToggle(prev &#x3D;&gt; !prev);</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  return [toggle, toggleHandler];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="8-useDebounce"><a href="#8-useDebounce" class="headerlink" title="8. useDebounce()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useDebounce">8. useDebounce()</a></h2><h3 id="题目-7"><a href="#题目-7" class="headerlink" title="题目"></a>题目</h3><p>For a frequently changing value like text input you might want to debounce the changes.</p>
<p>Implement <code>useDebounce()</code> to achieve this.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function App() &#123;</span><br><span class="line">  const [value, setValue] &#x3D; useState(...)</span><br><span class="line">  &#x2F;&#x2F; this value changes frequently, </span><br><span class="line">  const debouncedValue &#x3D; useDebounce(value, 1000)</span><br><span class="line">  &#x2F;&#x2F; now it is debounced</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>The logic should be similar to <a target="_blank" rel="noopener" href="https://bigfrontend.dev/problem/implement-basic-debounce">6. implement basic debounce()</a></p>
<h3 id="我的答案-7"><a href="#我的答案-7" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useState, useEffect &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useDebounce&lt;T&gt;(value: T, delay: number): T &#123;</span><br><span class="line">  const [debounced, setDebounced] &#x3D; useState&lt;T&gt;(value);</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    const timer &#x3D; setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      setDebounced(value);</span><br><span class="line">    &#125;, delay);</span><br><span class="line"></span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      clearTimeout(timer);</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;, [value, delay]);</span><br><span class="line">  return debounced;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="9-useEffectOnce"><a href="#9-useEffectOnce" class="headerlink" title="9. useEffectOnce()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useEffectOnce">9. useEffectOnce()</a></h2><h3 id="题目-8"><a href="#题目-8" class="headerlink" title="题目"></a>题目</h3><p>Here is a simple problem, implement <code>useEffectOnce()</code> as the name says itself, it runs an effect only once.</p>
<h3 id="我的答案-8"><a href="#我的答案-8" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useEffect, EffectCallback &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useEffectOnce(effect: EffectCallback) &#123;</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    return effect();</span><br><span class="line">  &#125;, []);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="10-phone-number-input"><a href="#10-phone-number-input" class="headerlink" title="10. phone number input"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/phone-number-input">10. phone number input</a></h2><h3 id="题目-9"><a href="#题目-9" class="headerlink" title="题目"></a>题目</h3><p>Create a <code>PhoneNumberInput</code> component.</p>
<ol>
<li>only accepts numerical digits</li>
<li>format the number automatically as <code>(123)456-7890</code> by</li>
</ol>
<ul>
<li>adding the parenthesis when the 4th digit is entered</li>
<li>also adding <code>-</code> before 7th digit</li>
</ul>
<p>You can use the default text input without any styling.</p>
<h3 id="Follow-up"><a href="#Follow-up" class="headerlink" title="Follow-up"></a>Follow-up</h3><p>What if user removes some digits in the middle, does caret jumps to the end in your approach?</p>
<blockquote>
<p>caret position is not covered in our tests.</p>
</blockquote>
<h3 id="我的答案-9"><a href="#我的答案-9" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123;useState, ChangeEvent&#125; from &#39;react&#39;</span><br><span class="line">export function PhoneNumberInput() &#123;</span><br><span class="line">  const [value, setValue] &#x3D; useState&lt;string&gt;(&quot;&quot;);</span><br><span class="line">  const handleChange &#x3D; (e: ChangeEvent&lt;HTMLInputElement&gt;) &#x3D;&gt; &#123;</span><br><span class="line">    const initialValue &#x3D; e.target.value;</span><br><span class="line">    let value: string &#x3D; initialValue.replace(&#x2F;\D&#x2F;g, &quot;&quot;);</span><br><span class="line">    if (value.length &gt; 0 &amp;&amp; value.length &lt;&#x3D; 3) &#123;</span><br><span class="line">      value &#x3D; value.replace(&#x2F;^(\d&#123;1,3&#125;)$&#x2F;g, &quot;$1&quot;);</span><br><span class="line">    &#125; else if (value.length &gt; 3 &amp;&amp; value.length &lt;&#x3D; 6) &#123;</span><br><span class="line">      value &#x3D; value.replace(&#x2F;^(\d&#123;3&#125;)(\d&#123;0,3&#125;)$&#x2F;g, &quot;($1)$2&quot;);</span><br><span class="line">    &#125; else if (value.length &gt; 6) &#123;</span><br><span class="line">      value &#x3D; value.replace(&#x2F;^(\d&#123;3&#125;)(\d&#123;3&#125;)(\d&#123;0,4&#125;)\d*$&#x2F;g, &quot;($1)$2-$3&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    setValue(value);</span><br><span class="line">  &#125;</span><br><span class="line">  return &lt;input data-testid&#x3D;&quot;phone-number-input&quot; onChange&#x3D;&#123;handleChange&#125; value&#x3D;&#123;value&#125;&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; if you want to try your code on the right panel</span><br><span class="line">&#x2F;&#x2F; remember to export App() component like below</span><br><span class="line"></span><br><span class="line">export function App() &#123;</span><br><span class="line">  return &lt;div&gt;&lt;PhoneNumberInput &#x2F;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="11-useFocus"><a href="#11-useFocus" class="headerlink" title="11. useFocus()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/phone-number-input">11. useFocus()</a></h2><h3 id="题目-10"><a href="#题目-10" class="headerlink" title="题目"></a>题目</h3><p>CSS pseudo-class <a target="_blank" rel="noopener" href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within">:focus-within</a> could be used to allow conditional rendering in parent element on the focus state of descendant elements.</p>
<p>While it is cool, in complex web apps, it might be better to control the state in script.</p>
<p>Now please create <code>useFocus()</code> to support this.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function App() &#123;</span><br><span class="line">  const [ref, isFocused] &#x3D; useFocus()</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;input ref&#x3D;&#123;ref&#125;&#x2F;&gt;</span><br><span class="line">    &#123;isFocused &amp;&amp; &lt;p&gt;focused&lt;&#x2F;p&gt;&#125;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="我的答案-10"><a href="#我的答案-10" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; Ref, useState, useRef, useCallback, useEffect &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">export function useFocus&lt;T extends HTMLInputElement&gt;(): [Ref&lt;T&gt;, boolean] &#123;</span><br><span class="line">  const ref &#x3D; useRef&lt;T&gt;(null);</span><br><span class="line">  const [isFocused, setIsFocused] &#x3D; useState&lt;boolean&gt;(false);</span><br><span class="line">  const toggle &#x3D; useCallback(() &#x3D;&gt; &#123;</span><br><span class="line">    setIsFocused(!isFocused);</span><br><span class="line">  &#125;, [isFocused]);</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    const element &#x3D; ref.current;</span><br><span class="line"></span><br><span class="line">    element?.addEventListener(&#39;focus&#39;, toggle);</span><br><span class="line">    element?.addEventListener(&#39;blur&#39;, toggle);</span><br><span class="line"></span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      element?.removeEventListener(&#39;focus&#39;, toggle);</span><br><span class="line">      element?.removeEventListener(&#39;blur&#39;, toggle);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  return [ref, isFocused];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; if you want to try your code on the right panel</span><br><span class="line">&#x2F;&#x2F; remember to export App() component like below</span><br><span class="line"></span><br><span class="line">export function App() &#123;</span><br><span class="line">  const [ref, isFocused] &#x3D; useFocus()</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;input ref&#x3D;&#123;ref&#125;&#x2F;&gt;</span><br><span class="line">    &#123;isFocused &amp;&amp; &lt;p&gt;focused&lt;&#x2F;p&gt;&#125;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="12-useArray"><a href="#12-useArray" class="headerlink" title="12. useArray()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react/useArray">12. useArray()</a></h2><h3 id="题目-11"><a href="#题目-11" class="headerlink" title="题目"></a>题目</h3><p>When array is used in React state, we need to deal with actions such as <code>push</code> and <code>remove</code>.</p>
<p>Implement <code>useArray()</code> to make life easier.</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;value, push, removeByIndex&#125; = useArray([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br></pre></td></tr></table></figure>

<h3 id="我的答案-11"><a href="#我的答案-11" class="headerlink" title="我的答案"></a>我的答案</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&#x2F;&#x2F; This is a React problem from BFE.dev</span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useCallback &#125; from &#39;react&#39;</span><br><span class="line"></span><br><span class="line">type UseArrayActions&lt;T&gt; &#x3D; &#123;</span><br><span class="line">  push: (item: T) &#x3D;&gt; void,</span><br><span class="line">  removeByIndex: (index: number) &#x3D;&gt; void</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function useArray&lt;T&gt;(initialValue: T[]): &#123; value: T[] &#125; &amp; UseArrayActions&lt;T&gt; &#123;</span><br><span class="line">  const [value, setValue] &#x3D; useState&lt;T[]&gt;(initialValue);</span><br><span class="line"></span><br><span class="line">  const push &#x3D; useCallback((item: T) &#x3D;&gt; &#123;</span><br><span class="line">    setValue((preValue) &#x3D;&gt; &#123;</span><br><span class="line">      return [...preValue, item];</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  const removeByIndex &#x3D; useCallback((index: number) &#x3D;&gt; &#123;</span><br><span class="line">    setValue((preValue) &#x3D;&gt; &#123;</span><br><span class="line">      const newValue &#x3D; [...preValue];</span><br><span class="line">      newValue.splice(index, 1);</span><br><span class="line">      return newValue;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  return &#123;</span><br><span class="line">    value,</span><br><span class="line">    push,</span><br><span class="line">    removeByIndex</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; if you want to try your code on the right panel</span><br><span class="line">&#x2F;&#x2F; remember to export App() component like below</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; export function App() &#123;</span><br><span class="line">&#x2F;&#x2F; const &#123; value &#125; &#x3D; useArray([1, 2, 3])</span><br><span class="line">&#x2F;&#x2F; return &lt;div&gt;</span><br><span class="line">&#x2F;&#x2F;   &#123;value&#125;</span><br><span class="line">&#x2F;&#x2F; &lt;&#x2F;div&gt;</span><br><span class="line">&#x2F;&#x2F; &#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/React/" rel="tag"><i class="fa fa-tag"></i> React</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 前端</a>
              <a href="/tags/bigfrontend/" rel="tag"><i class="fa fa-tag"></i> bigfrontend</a>
              <a href="/tags/%E9%A2%98%E5%BA%93/" rel="tag"><i class="fa fa-tag"></i> 题库</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/" rel="prev" title="bigfrontend的React quizzes">
      <i class="fa fa-chevron-left"></i> bigfrontend的React quizzes
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/" rel="next" title="力扣周赛301-">
      力扣周赛301- <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#React-Problems"><span class="nav-text">React Problems</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-the-React-Counter-app"><span class="nav-text">1. the React Counter app</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-useTimeout"><span class="nav-text">2. useTimeout()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-1"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-1"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-useIsFirstRender"><span class="nav-text">3. useIsFirstRender()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-2"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-2"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-useSWR-I"><span class="nav-text">4. useSWR() I</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-3"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-3"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-usePrevious"><span class="nav-text">5. usePrevious()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-4"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-4"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-useHover"><span class="nav-text">6. useHover()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-5"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-5"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-useToggle"><span class="nav-text">7. useToggle()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-6"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-6"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-useDebounce"><span class="nav-text">8. useDebounce()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-7"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-7"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-useEffectOnce"><span class="nav-text">9. useEffectOnce()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-8"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-8"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-phone-number-input"><span class="nav-text">10. phone number input</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-9"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Follow-up"><span class="nav-text">Follow-up</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-9"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-useFocus"><span class="nav-text">11. useFocus()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-10"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-10"><span class="nav-text">我的答案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-useArray"><span class="nav-text">12. useArray()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-11"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%AD%94%E6%A1%88-11"><span class="nav-text">我的答案</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
